const BackPressureTransform = require("./BackPressureTransform");
const moment = require("moment");
module.exports = class LogStream extends BackPressureTransform {
  constructor(opts) {
    opts = Object.assign({
      label: "LogStream",
      interval: 1, // 隔几次log调用输出一次数据
      step: 1, // 调用一次log表示多少个数据   interval=2,step=30  一个班30个人，走过之后运行一次log，过了两个班才真正输出一次
    }, opts);
    super(opts);
    this.opts = opts;
    this.n = 1;
  }

  _transform(chunks, encoding, callback) {
    const { interval, label, step } = this.opts;
    const length = chunks.length;
    if (this.n % interval === 0 || length < step) {
      const count = (this.n - 1) * step + length;
      console.log(`[${moment().format("YYYY-MM-DD HH:mm:ss")}]:${label} 同步了${this.n}次，${count}条数据`);
    }
    this.n++;
    this.backPressurePush(chunks, callback);
  }
};
